import axios from 'axios';
import store from '../store'
import {
    Message
} from 'element-ui'
//请求前缀

const service = axios.create({
    // baseURL: "/image-identification",
    baseURL: '/aiplatform',
});

//请求拦截器
service.interceptors.request.use(function (config) {
    //在发送请求前
    //设置请求头
    //config.headers['token'] = '';
    // config.headers.common["token"] = store.getters.token;
    let params = config.params || {};
    let data = config.data || {};
    // 默认所有请求都会加上creater，如果参数中noCreate为true，则不传creater
    const noCreater = params.noCreater || data.noCreater;

    // params.creater = store.getters.userName;
    if (noCreater) {
        delete params.creater;
    }
    config.params = params;

    if (config.method === 'post') {
        // data.creater = store.getters.userName;
        if (noCreater) {
            delete data.creater;
        }
        config.data = data;
    }
    // 将token塞入每个请求
    if (config.url.startsWith('/login')) {
        return config
    } else {
        config.headers.token = window.sessionStorage.getItem("token");
    }
    return config;
}, function (error) {
    //请求错误
    return Promise.reject(error)
})

//响应拦截

service.interceptors.response.use(function (response) {
    //响应数据
    // console.log(response);
    return response;
}, function (error) {
    Message({
        message: error.message,
        type: 'error',
        duration: 2000
    });
    //响应数据错误
    return Promise.reject(error)
})

export default service; //直接返回 axios;
